<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use think\Response;
use think\exception\ValidateException;
use app\common\model\Activity as ActivityModel;
use app\common\model\UserActivity;

class Activity extends Base
{
    /**
     * 详情
     */
    public function info($id)
    {
        try {
            $info = ActivityModel::where(['id' => $id])->field('id,title,images,hd_s_time,hd_e_time,bm_s_time,bm_e_time,content')->find();
            if (empty($info)) {
                return Result::fail('信息不存在！');
            }
            $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
            $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
            $info->content = str_replace('\\', '', $info->content);
            $info->activity_time = date('Y年m月d日', $info->hd_s_time) . '-' . date('m月d日', $info->hd_e_time);
            $info->count_down = ($info->bm_e_time - time()) > 0 ? $info->bm_e_time - time() : 0;
            $info->down_title = '距离报名结束还剩';
            if ($info->bm_s_time > time()) {
                $info->status = 1; //报名未开始
                $info->status_name = '未开始'; //报名未开始
            } elseif ($info->bm_e_time < time()) {
                $info->status = 2; //报名已结束
                $info->status_name = '已结束'; //报名已结束
            } else {
                $info->status = 3; //报名中
                $info->status_name = '报名中'; //报名中
            }
            return Result::success('获取成功', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 列表
     */
    public function getList(): Response
    {
        try {
            $page = $this->request->get('page', 1);
            $list = ActivityModel::getPageList([], 'id,title,images,hd_s_time,hd_e_time,bm_s_time,bm_e_time', $page, 20, false);
            foreach ($list['data'] as &$v) {
                $v->activity_time = date('Y年m月d日', $v->hd_s_time) . '-' . date('m月d日', $v->hd_e_time);
                $v->down_title = '距结束还剩：';
                if ($v->bm_s_time > time()) {
                    $v->down_title = '距开始还剩：';
                    $v->status = 1; //报名未开始
                    $v->status_name = '未开始'; //报名未开始
                    $v->count_down = ($v->bm_s_time - time()) > 0 ? $v->bm_s_time - time() : 0;
                } elseif ($v->bm_e_time < time()) {
                    $v->status = 2; //报名已结束
                    $v->status_name = '已结束'; //报名已结束
                    $v->count_down = 0;
                } else {
                    $v->status = 3; //报名中
                    $v->status_name = '立即报名'; //报名中
                    $v->count_down = ($v->bm_e_time - $v->bm_s_time) > 0 ? $v->bm_e_time - $v->bm_s_time : 0;
                }
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 提交
     */
    public function submit(): Response
    {
        try {
            $param = $this->request->post();
            $this->validate($param, 'Activity');
            return Result::fastReturn(UserActivity::submit($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取我的报名
     */
    public function getMyList(): Response
    {
        try {
            $param = $this->request->get();
            $type = $param['type'];
            if (isset($type) && $type != 0) {
                if ($type == 1) {
                    $map[] = ['bm_s_time', '<=', time()];
                    $map[] = ['bm_e_time', '>=', time()];
                } else if ($type == 2) {
                    $map[] = ['bm_e_time', '<=', time()];
                }
            }
            $activity_id = UserActivity::where('uid',$this->user->id)->column('activity_id');
            $map[] = ['id', 'in', $activity_id];
            // print('<pre>');print_r($activity_id);die;
            $list = ActivityModel::getPageList($map, 'id,title,images,hd_s_time,hd_e_time,bm_s_time,bm_e_time', $param['page'], 20, false,[],[],[],[],[],'',['sign_text']);
            foreach ($list['data'] as &$v) {    
                $v->activity_time = date('Y年m月d日', $v->hd_s_time) . '-' . date('m月d日', $v->hd_e_time);
                $v->create_time = UserActivity::where(['uid'=>$this->user->id,'activity_id'=>$v->id])->value('create_time');
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
